package com.codeoss.module.stock.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.stock.dao.StockStorageMapper;
import com.codeoss.module.stock.model.StockStorage;
import com.codeoss.module.stock.service.IStockStorageService;

@Service
public class StockStorageService extends BaseService<StockStorageMapper, StockStorage> implements IStockStorageService {

	@Override
	@Transactional
	public void add(Long warehouseId, Long loatorId, Long productId, Long attributeSetInstanceId,
			BigDecimal diffQtyOnHand, BigDecimal diffQtyReserved, BigDecimal diffQtyOrdered) {
		// TODO Auto-generated method stub
		StockStorage stockStorage=getCreateStockStorage(loatorId, productId, attributeSetInstanceId);
		if(diffQtyOnHand!=null) {
			stockStorage.setQtyOnhand(stockStorage.getQtyOnhand().add(diffQtyOnHand));
		}
		if(diffQtyReserved!=null) {
			stockStorage.setQtyReserved((stockStorage.getQtyReserved().add(diffQtyReserved)));
		}
		if(diffQtyOrdered!=null) {
			stockStorage.setQtyOrdered((stockStorage.getQtyOrdered().add(diffQtyOrdered)));
		}
		update(stockStorage);
	}

	@Transactional
	protected StockStorage getCreateStockStorage(Long loatorId, Long productId,Long attributeSetInstanceId) {
		QueryWrapper<StockStorage> queryWrapper=new QueryWrapper<StockStorage>();
		queryWrapper.eq("stock_locator_id", loatorId);
		queryWrapper.eq("product_product_id", productId);
		if(attributeSetInstanceId!=null && attributeSetInstanceId.longValue()!=0) {
			queryWrapper.eq("product_attr_set_instance_id", attributeSetInstanceId);
		}else {
			queryWrapper.isNull("product_attr_set_instance_id");
		}
		StockStorage stockStorage=getOne(queryWrapper);
		if(stockStorage==null) {
			stockStorage=new StockStorage();
			stockStorage.setStockLocatorId(loatorId);
			stockStorage.setProductProductId(productId);
			stockStorage.setProductAttrSetInstanceId(attributeSetInstanceId);
			stockStorage.setQtyOnhand(new BigDecimal(0));
			stockStorage.setQtyOrdered(new BigDecimal(0));
			stockStorage.setQtyReserved(new BigDecimal(0));
			insert(stockStorage);
		}
		return stockStorage;
	}
	
	@Override
	public BigDecimal getQtyBook(Long loatorId, Long productId, Long attributeSetInstanceId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockStorage> queryWrapper=new QueryWrapper<StockStorage>();
		queryWrapper.eq("stock_locator_id", loatorId);
		queryWrapper.eq("product_product_id", productId);
		if(attributeSetInstanceId!=null && attributeSetInstanceId.longValue()!=0) {
			queryWrapper.eq("product_attribute_set_instance_id", attributeSetInstanceId);
		}
		queryWrapper.select("sum(qty_onhand) as qtyhand");
		Map<String,Object> dataMap=getMap(queryWrapper);
		BigDecimal qtyBook=new BigDecimal(0);
		if(dataMap!=null) {
			qtyBook=(BigDecimal)dataMap.get("qtyhand");
		}
		return qtyBook;
	}

	@Override
	public List<StockStorage> getAvailableStockStorageList(Long productProductId, Long stockLocatorId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockStorage> query=new QueryWrapper<StockStorage>();
		query.eq("product_product_id", productProductId);
		query.eq("stock_locator_id", stockLocatorId);
		query.gt("qty_onhand", new BigDecimal(0));
		query.orderByAsc("product_attribute_set_instance_id");
		return list(query);
	}
}